<?php
namespace app\admin\controller;
use app\admin\model\Route as RouteModel;
use app\admin\model\Deriver as DeriverModel;
use app\admin\model\Menu as MenuModel;
use util\Tree;
use think\Validate;
use think\Cache;
use think\Db;

/**
 * 系统管理模块
 * @package app\admin\controller
 */
class Route extends Base
{
    /**
     * 路线设置
     * @author 陈一中
     * @return mixed
     */
    public function index()
    {
        // 数据列表
        $data_list = RouteModel::order('updatetime desc,id desc')->paginate();
        cookie('__forward__', $_SERVER['REQUEST_URI']);

        $this->assign('meta_title', '司机管理');
        $this->assign('list', $data_list);
        return $this->fetch();
    }

     /**
     * 添加路线
     * @author 陈一中 <[email address]>
     */
    public function add()
    {
    	 // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            $data['status'] = "1";
            $data['create_time'] = time();
            $data['updatetime'] = time();
            // 验证
            $result = $this->validate($data, 'Route');
            // 验证失败 输出错误信息
            if(true !== $result) return $this->error($result);
            // 添加数据
            if ($route = RouteModel::create($data)) {
                // 记录行为
                // action_log('user_update', 'user', $user['id']);
                return $this->success('添加成功', cookie('__forward__'));
            } else {
                return $this->error('添加失败');
            }
        }
        $this->assign('meta_title', '添加路线');
        return $this->fetch();
    }

    /**
     * 编辑路线信息
     * @author 陈一中 <[email address]>
     * @param  string $id [description]
     * @return [type]     [description]
     */
    public function edit($id = '0')
    {
        // 保存数据
        if ($this->request->isPost()) {
            $data = $this->request->post();
            // 验证
            $result = $this->validate($data, 'Route');
            // 验证失败 输出错误信息
            if (true !== $result) {
                return $this->error($result);
            }

            if ($route = RouteModel::where(['id'=>$id])->update($data)) {          
                // Cache::clear();
                // 记录行为
                action_log('route_update', 'route', $route['id']);
                $this->success('操作成功', cookie('__forward__'));
            } else {
                $this->error('操作失败', cookie('__forward__'));
            }
        }
        $this->assign('meta_title', '编辑文章');
        $info = RouteModel::get($id);
        $this->assign('info', $info);

        return $this->fetch();
    }
}
